import { createClass, registerClass } from "../../../cfw/tools/class_decorator";
import { SDKAdParam, SDKState } from "../../SDKDefine";
import { BaseAdMgr } from "../base/BaseAdMgr";
import { IAdAdapter } from "../base/IAdAdapter";

@registerClass('SingleAdMgr')
export class SingleAdMgr extends BaseAdMgr {


    protected ad: IAdAdapter;
    put(ad: IAdAdapter): void {
        this.ad = ad
    }
    open(param: SDKAdParam): void {
        let ad = this.ad;
        if (ad) {
            if (this.getCurIndex() != param.index) {
                this.destroy()
                this.setCurIndex(param.index)
            }
            ad.setID(this.data[param.index])
            ad.setParam(param)
            ad.open()
        }
    }


    get() {
        return this.ad
    }

    close(index: number): void {
        if (this.ad) {
            this.ad.close()
        }
    }



    destroy(): void {
        if (this.ad) {
            this.ad.destroy()
        }
    }

    has(): boolean {
        return this.ad != undefined;
    }

    init() {
        if (this.cfg && this.cfg.className) {
            let ins: IAdAdapter = createClass(this.cfg.className)
            if (ins) {
                ins.setSystem(this.getSystem())
                ins.setID(this.data[this.curIndex])
                ins.init()
                this.ad = ins;
            }
        }
    }
}